package com.example.myapplication.found.model;

import com.example.myapplication.found.bean.StockBean;
import com.example.myapplication.found.bean.StockKlineBean;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author: rongfeng
 * @date: 2022/2/17
 * @description
 */
public class ZhangDie {
    private StockBean stockBean;
    public List<StockKlineBean> klineList;
    private float money = 10000;

    public ZhangDie(StockBean stockBean) {
        this.stockBean = stockBean;
        this.klineList = stockBean.klineList;
    }

    public float start() {
        int count = 0;
        int diefu = 0;
        boolean isUP = false;
        int changeCount = 0;
        Map<Integer, Integer> mapZhang = new HashMap<>();
        Map<Integer, Integer> mapDie = new HashMap<>();
        Map<Integer, Integer> mapDieFu = new HashMap<>();
        for (int i = 0; i < klineList.size(); i++) {
            StockKlineBean bean = klineList.get(i);

            if (diefu == -5) {
                changeCount++;
                for (int j = 0; j <= 3; j++) {
                    if (i + j < klineList.size()) {
                        StockKlineBean nextBean = klineList.get(i + j);
                        money = money + (money * (nextBean.chg / 100));
                    }
                }
            }

            if (bean.chg >= 0) {
                if (!isUP) {
                    Integer num = mapDie.get(count);
                    if (num == null) {
                        num = 0;
                    }
                    mapDie.put(count, num + 1);

                    count = 0;
                    Integer diefunum = mapDieFu.get(diefu);
                    if (diefunum == null) {
                        diefunum = 0;
                    }
                    mapDieFu.put(diefu, diefunum + 1);
                    diefu = 0;
                }
                isUP = true;
                count++;
            } else if (bean.chg < 0) {
                if (isUP) {
                    Integer num = mapZhang.get(count);
                    if (num == null) {
                        num = 0;
                    }
                    mapZhang.put(count, num + 1);
                    count = 0;
                }
                diefu = diefu + (int) bean.chg;

                isUP = false;
                count++;
            }
        }

        count = 0;
        for (int i = 1; i <= 3; i++) {
            StockKlineBean bean = klineList.get(klineList.size() - i);
            if (bean.chg < 0) {
                count++;
            }
            if (count == 3) {
                System.out.println("找到连跌 " + stockBean.name + " " + stockBean.code + " " + klineList.get(klineList.size() - 1).date);
            }
        }


        System.out.println(stockBean.name + " " + stockBean.code + " 交易天数 " + klineList.size());

//        Iterator<Map.Entry<Integer, Integer>> it1 = mapZhang.entrySet().iterator();
//        while (it1.hasNext()) {
//            Map.Entry<Integer, Integer> entry = it1.next();
//            System.out.println("连续涨: " + entry.getKey() + "  次数: " + entry.getValue());
//        }
//        System.out.println(" ");


//        Iterator<Map.Entry<Integer, Integer>> it2 = mapDie.entrySet().iterator();
//        count = 0;
//        while (it2.hasNext()) {
//            Map.Entry<Integer, Integer> entry = it2.next();
//            count = count + entry.getValue();
//        }
//        it2 = mapDie.entrySet().iterator();
//        while (it2.hasNext()) {
//            Map.Entry<Integer, Integer> entry = it2.next();
//            DecimalFormat decimalFormat = new DecimalFormat("0.00");//构造方法的字符格式这里如果小数不足2位,会以0补足.
//            String p2 = decimalFormat.format(((float) entry.getValue() / (float) count) * 100);
//            System.out.println("连续跌: " + entry.getKey() + "  次数: " + entry.getValue() + "  " + p2 + "%");
//        }
//
//        System.out.println(" ");
//        it2 = mapDieFu.entrySet().iterator();
//        count = 0;
//        while (it2.hasNext()) {
//            Map.Entry<Integer, Integer> entry = it2.next();
//            count = count + entry.getValue();
//        }
//        it2 = mapDieFu.entrySet().iterator();
//        while (it2.hasNext()) {
//            Map.Entry<Integer, Integer> entry = it2.next();
//            DecimalFormat decimalFormat = new DecimalFormat("0.00");//构造方法的字符格式这里如果小数不足2位,会以0补足.
//            String p2 = decimalFormat.format(((float) entry.getValue() / (float) count) * 100);
//            System.out.println("连续跌幅度: " + entry.getKey() + "  次数: " + entry.getValue() + "  " + p2 + "%");
//        }

        System.out.println("交易次数 " + changeCount + " 最终金额 " + money);
        System.out.println(" ");

        return money;

    }


}
